<template>
  <div>
    <!-- 顶部 -->
    <el-card body-style="firstCard">
      <el-form :inline="true">
        <el-form-item label="机构名称">
          <el-input
            placeholder="请输入机构名称"
            style="width: 500px"
            v-model="searchName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="searchOrg">
            搜索
          </el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Plus" @click="addOrg">
            添加机构
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 卡片2 -->
    <el-card style="margin-top: 20px">
      <el-table :data="IbdStore.orgList" border>
        <el-table-column
          label="序号"
          align="center"
          type="index"
          width="80px"
        ></el-table-column>
        <el-table-column label="机构名称" align="center" width="600px">
          <template #default="scope">
            <el-link :underline="false" @click="orgDetail(scope.row)">
              {{ scope.row.fullname }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="机构类型"
          align="center"
          prop="type"
        ></el-table-column>
        <el-table-column
          label="联系状态"
          align="center"
          prop="status"
        ></el-table-column>
      </el-table>
      <el-pagination
        :total="total"
        v-model:current-page="pageNo"
        v-model:page-size="pageSize"
        @current-change="handleCurrentChange"
        layout="prev,pager,next"
      ></el-pagination>
    </el-card>
  </div>
  <!-- 添加信息框 -->
  <el-drawer
    v-model="showDrawer"
    title="添加机构"
    :before-close="handleClose"
    size="40%"
  >
    <el-form :model="formOrg" :rules="rules">
      <el-form-item label="机构名称" prop="fullname">
        <el-input
          v-model="formOrg.fullname"
          placeholder="请输入机构名称"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="机构类型" prop="type">
        <el-select v-model="formOrg.type" placeholder="请选择机构类型">
          <el-option label="城投企业" value="cityInvest" />
          <el-option label="产投企业" value="bizInvest" />
          <el-option label="金融机构" value="finInst" />
          <el-option label="评级机构" value="creditor" />
          <el-option label="审计机构" value="auditor" />
          <el-option label="律师事务所" value="lawfirm" />
          <el-option label="其他" value="other" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系状态" prop="status">
        <el-select v-model="formOrg.status" placeholder="请选择联系状态">
          <el-option label="建立联系" value="1" />
          <el-option label="初步沟通" value="2" />
          <el-option label="开展项目" value="3" />
          <el-option label="失去联系" value="0" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="flex: auto">
        <!-- <el-button @click="cancelClick">取消</el-button> -->
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { reqOrganizationList } from '@/api/ibd'
import useIbdStore from '@/store/modules/ibd'
import { ElMessageBox } from 'element-plus'
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'

let IbdStore = useIbdStore()
let $router = useRouter()
let showDrawer = ref<boolean>(false)
let searchName = ref<string>('')
let formOrg = ref({
  fullname: '',
  type: '',
  status: '',
})
let total = ref<number>(0)
let pageNo = ref<number>(1)
let pageSize = ref<number>(5)

const rules = {
  fullname: [{ required: true, message: '请输入机构名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择机构类型', trigger: 'blur' }],
  status: [{ required: true, message: '请选择联系状态', trigger: 'blur' }],
}

const resetSearch = () => {
  searchName.value = ''
  getOrgList()
}

const searchOrg = () => {
  getOrgList()
}

const addOrg = () => {
  showDrawer.value = true
  formOrg.value.fullname = ''
  formOrg.value.type = ''
  formOrg.value.status = ''
}
const handleClose = (done: () => void) => {
  ElMessageBox.confirm('确定不保存就关闭么？')
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
const handleCurrentChange = async (pager: number) => {
  // console.log(pager);
  getOrgList(pager)
  // await IbdStore.getOrganiazaionList(pageInfo.psize, val)
  // pageInfo.current = val
}
const submitForm = async () => {
  await IbdStore.addOrganization(formOrg.value)
  showDrawer.value = false
  // await IbdStore.getOrganiazaionList(pageInfo.psize, pageInfo.current)
}
const orgDetail = (row) => {
  console.log(row)
  $router.push(`/ibd/organization/detail/${row._id}`)
}

const getOrgList = async (pager = 1) => {
  pageNo.value = pager
  // searchName.value = search
  let resp = await reqOrganizationList(
    pageSize.value,
    pageNo.value,
    searchName.value,
  )
  if (resp.code === 200) {
    total.value = resp.data.total
    pageNo.value = resp.data.currentPage
    IbdStore.getOrganiazaionList(resp.data.data)
    // console.log("$$$$", IbdStore.orgList);
  }
}

onMounted(() => {
  //   IbdStore.getOrganiazaionList(pageInfo.psize, pageInfo.current)
  getOrgList()
})
</script>
<style scoped lang="scss">
.el-pagination {
  justify-content: center;
  margin-top: 5px;
}
</style>
